/*
 *  Copyright (C) 2011 Matt Van Der Westhuizen <mattpwest@gmail.com>
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.chaoticengine.cgll.profile;

import java.io.File;
import org.simpleframework.xml.Serializer;

/**
 * The profile manager manages the list of profiles for the game.
 *
 * @author Matt Van Der Westhuizen <mattpwest@gmail.com>
 */
public class ProfileManager {
    private static ProfileManager instance = null;
    private Profiles profiles = null;
    
    /** The currently active profile. */
    private Profile profile = null;

    protected ProfileManager() {
    }

    public static ProfileManager getInstance() {
        if (instance == null) {
            instance = new ProfileManager();
        }

        return(instance);
    }

    public Profile getProfile() {
        return(profile);
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }

    public Profiles getProfiles() {
        if (profiles == null) {
            profiles = new Profiles();
        }
        
        return(profiles);
    }

    public void loadProfiles(Serializer serial, String filename) {
        try {
            profiles = serial.read(Profiles.class, new File(filename));
        } catch (Exception ex) {
            System.out.println("WARNING: Failed to load profiles from '" + filename + "'.");
            ex.printStackTrace();
            profiles = new Profiles();
        }
    }

    public void saveProfiles(Serializer serial, String filename) throws Exception {
        serial.write(this.profiles, new File(filename));
    }
}
